<?php

use PHPUnit\Framework\TestCase;

use Elementor\Modules\Interactions\Parser;

/**
 * Due to the fact that original implementation is utilizing the static method call
 * of the Utils::generate_id, we need to override the original implementation to
 * use in tests
 */
class Parser_Ex extends Parser {
	private $__increment = 0;

	protected function get_next_interaction_id( $prefix ) {
		++$this->__increment;
		return implode( '-', [ $this->post_id, $prefix, $this->__increment ] );
	}
}

/**
 *
 * @group Elementor\Modules
 * @group Elementor\Modules\Interactions
 * @group Elementor\Modules\Interactions\Parser
 *
 */
class Test_Parser extends TestCase {
	const DOCUMENT_ID = 1;

	private function parser() {
		return new Parser_Ex( self::DOCUMENT_ID );
	}

	private function given_document_without_interactions() {
		return [
			'elements' => [
				[
					'id' => '1',
					'elType' => 'e-flexbox',
					'settings' => [],
					'elements' => [
						[
							'id' => '2',
							'elType' => 'widget',
							'widgetType' => 'e-heading',
						],
					],
				],
			],
		];
	}

	public function test_assign_interaction_ids__will_ignore_elements_without_interactions() {
		$document = $this->given_document_without_interactions();

		$result = $this->parser()->assign_interaction_ids( $document );

		$this->assertEquals( [
			'elements' => [
				[
					'id' => '1',
					'elType' => 'e-flexbox',
					'settings' => [],
					'elements' => [
						[
							'id' => '2',
							'elType' => 'widget',
							'widgetType' => 'e-heading',
						],
					],
				],
			],
		], $result );
	}

	public function test_assign_interactions_ids__will_augment_elements_with_interactions() {
		$given_document = [
			'elements' => [
				[
					'id' => '1',
					'elType' => 'e-flexbox',
					'settings' => [],
					'interactions' => json_encode( [
						'items' => [
							[
								'animation' => [
									'animation_type' => 'full-preset',
									'animation_id' => 'load-fade-in--100-0',
								],
							],
						],
						'version' => 1,
					] ),
				],
			],
		];

		$result = $this->parser()->assign_interaction_ids( $given_document );

		$this->assertEquals( [
			'elements' => [
				[
					'id' => '1',
					'elType' => 'e-flexbox',
					'settings' => [],
					'interactions' => json_encode( [
						'items' => [
							[
								'interaction_id' => '1-1-1',
								'animation' => [
									'animation_type' => 'full-preset',
									'animation_id' => 'load-fade-in--100-0',
								],
							],
						],
						'version' => 1,
					] ),
				],
			],
		], $result );
	}

	public function test_assign_interactions_ids__will_not_override_existing_interaction_ids() {
		$given_document = [
			'elements' => [
				[
					'id' => '1',
					'elType' => 'e-flexbox',
					'settings' => [],
					'interactions' => json_encode( [
						'items' => [
							[
								'interaction_id' => 'existing-interaction-id',
								'animation' => [
									'animation_type' => 'full-preset',
									'animation_id' => 'load-fade-in--100-0',
								],
							],
							[
								'animation' => [
									'animation_type' => 'full-preset',
									'animation_id' => 'load-fade-in--200-0',
								],
							],
						],
						'version' => 1,
					] ),
				],
			],
		];

		$result = $this->parser()->assign_interaction_ids( $given_document );

		$this->assertEquals( [
			'elements' => [
				[
					'id' => '1',
					'elType' => 'e-flexbox',
					'settings' => [],
					'interactions' => json_encode( [
						'items' => [
							[
								'interaction_id' => 'existing-interaction-id',
								'animation' => [
									'animation_type' => 'full-preset',
									'animation_id' => 'load-fade-in--100-0',
								],
							],
							[
								'interaction_id' => '1-1-1',
								'animation' => [
									'animation_type' => 'full-preset',
									'animation_id' => 'load-fade-in--200-0',
								],
							],
						],
						'version' => 1,
					] ),
				],
			],
		], $result );
	}
}
